package com.lemon.smartsafetynew.ui.adapter.incident;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafetynew.entity.common.DateTypeEnum;
import com.lemon.smartsafetynew.entity.incident.IncidentVo;
import com.lemon.smartsafetynew.util.common.CommonUtil;
import com.lemon.smartsafetynew.util.incident.IncidentUtil;

import java.util.List;

import butterknife.Bind;
import kale.adapter.LemonRcvAdapter;
import kale.adapter.item.AdapterItem;

/**
 * 事件 RecyclerView Adapter
 * Created by Andrew on 2017/9/6.
 */

public class IncidentListAdapter extends LemonRcvAdapter<IncidentVo> {

    private RecyclerTClickListener<IncidentVo> mListener;

    public IncidentListAdapter(@Nullable List<IncidentVo> data,
                               RecyclerTClickListener<IncidentVo> mListener) {
        super(data);
        this.mListener = mListener;
    }

    @NonNull
    @Override
    public AdapterItem createItem(Object type) {
        return new ItemAdapter();
    }

    private class ItemAdapter implements AdapterItem<IncidentVo> {

        private IncidentVo mData;
        private int mPosition;

        private ImageView incidentImageView;
        private TextView incidentTitleTv;

        private TextView incidentLabel1NameTv;
        private TextView incidentLabel1ContentTv;

        private TextView incidentLabel2NameTv;
        private TextView incidentLabel2ContentTv;

        private TextView incidentLabel3NameTv;
        private TextView incidentLabel3ContentTv;

        private RelativeLayout relativeLayout;
        private LinearLayout linearLayout;

        @Override
        public int getLayoutResId() {
            return R.layout.item_incident;
        }

        @Override
        public void bindViews(View root) {

            incidentImageView = (ImageView) root.findViewById(R.id.incident_imageView);
            incidentTitleTv = (TextView) root.findViewById(R.id.incident_title_tv);
            incidentLabel1NameTv = (TextView) root.findViewById(R.id.incident_label_1_name_tv);
            incidentLabel1ContentTv = (TextView) root.findViewById(R.id.incident_label_1_content_tv);
            incidentLabel2NameTv = (TextView) root.findViewById(R.id.incident_label_2_name_tv);
            incidentLabel2ContentTv = (TextView) root.findViewById(R.id.incident_label_2_content_tv);
            incidentLabel3NameTv = (TextView) root.findViewById(R.id.incident_label_3_name_tv);
            incidentLabel3ContentTv = (TextView) root.findViewById(R.id.incident_label_3_content_tv);
            relativeLayout = (RelativeLayout) root.findViewById(R.id.relative_layout);
            linearLayout = (LinearLayout) root.findViewById(R.id.linear_layout);

        }

        @Override
        public void setViews() {
            relativeLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mListener.onItemClick(mData, mPosition);
                }
            });
        }

        @Override
        public void handleData(IncidentVo incidentVo, int position) {

            mData = incidentVo;
            mPosition = position;

            incidentImageView.setBackgroundResource(IncidentUtil.getIncidentSourceImage(incidentVo.getSource_type()));
            incidentTitleTv.setText(IncidentUtil.getIncidentSourceStrRes(incidentVo.getSource_type()));
            linearLayout.setBackgroundResource(IncidentUtil.getIncidentBackground(incidentVo.getStatus()));
            incidentLabel1ContentTv.setText(CommonUtil.avoidNullString(incidentVo.getName()));  //检查部位
            incidentLabel2ContentTv.setText(CommonUtil.avoidNullString(incidentVo.getReport_user_name()));  //检查人
            incidentLabel3ContentTv.setText(CommonUtil.secondToTimeStr(incidentVo.getReport_time(), DateTypeEnum.DAY));  //检查时间

        }

    }
}
